package com.mazaiting.instructions.stores;

import com.mazaiting.instructions.base.Index8Instruction;
import com.mazaiting.instructions.base.NoOperandsInstruction;
import com.mazaiting.rtda.MtFrame;

/**
 * 变量从操作数栈顶弹出，然后存入局部变量表
 *
 * @author mazaiting
 * @since 2021/8/14
 */
public class LStoreInstruction {

    /**
     * 变量从操作数栈顶弹出，然后存入局部变量表
     * @param frame 栈帧
     * @param index 索引
     */
    private static void store(MtFrame frame, int index) {
        // 变量从操作数栈顶弹出
        long num = frame.getOperandStack().popLong();
        // 推入操作数栈顶
        frame.getLocalVars().setLong(index, num);
    }

    public static class LStore extends Index8Instruction {
        @Override
        public void execute(MtFrame frame) {
            store(frame, index);
        }
    }

    public static class LStore0 extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            store(frame, 0);
        }
    }

    public static class LStore1 extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            store(frame, 1);
        }
    }

    public static class LStore2 extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            store(frame, 2);
        }
    }

    public static class LStore3 extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            store(frame, 3);
        }
    }

    public static class LStore4 extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            store(frame, 4);
        }
    }

}
